package com.hst.capacity.facade.rest;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hst.capacity.application.biz.IntercityLineBiz;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.setting.line.IntercityLineAutoGrabEntity;
import com.hst.capacity.domain.model.request.setting.line.CityLineParam;
import com.hst.capacity.domain.model.request.setting.line.IcLineSaveParam;
import com.hst.capacity.domain.model.request.setting.line.IntercityLineAutoGrabParam;
import com.hst.capacity.domain.model.request.setting.price.IcSaveParam;
import com.hst.capacity.domain.model.response.DefaultVO;
import com.hst.capacity.domain.model.response.setting.line.AutoGrabRuleVO;
import com.hst.capacity.domain.model.response.setting.line.IntercityLineAutoGrabVO;
import com.hst.capacity.infra.convertor.IntercityConvertor;
import com.hst.capacity.infra.util.NumUtil;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.hst.capacity.domain.model.response.DefaultVO.success;

/**
 *  城际订单录入模块Controller，城际订单 CRUD，检索等。
 *
 * */
@RequestMapping("/intercity/line")
@RestController
public class IntercityLineController {

    @Autowired
    private IntercityLineBiz icLineBiz;

    /**
     * 城际线路信息取得
     * @param param
     * @return
     */
    @PostMapping("/cityLine")
    private DefaultVO cityLineInfo(@RequestBody Map<String,String> param){
        String idCityLine = param.get("idCityLine");
        if (StrUtil.isEmpty(idCityLine))
            throw new BusinessException(4001,"城市线路ID[idCityLine]不能为空");
        return success(icLineBiz.getCityLine(idCityLine));
    }

    /**
     * 城际线路信息取得
     * @param param
     * @return
     */
    @PostMapping("/cityLine/save")
    private DefaultVO cityLineSave(@RequestBody CityLineParam param){
        return success(icLineBiz.cityLineSave(param));
    }

    /**
     * 城际线路信息取得
     * @param param
     * @return
     */
    @PostMapping("/info")
    private DefaultVO icLineInfo(@RequestBody Map<String,String> param){
        String idLine = param.get("idLine");
        if (StrUtil.isEmpty(idLine))
            throw new BusinessException(4001,"线路[idLine]不能为空");
        return success(icLineBiz.getInterCityLine(idLine));
    }

    /**
     * 城际线路信息保存
     * @param param
     * @return
     */
    @PostMapping("/save")
    private DefaultVO save(@RequestBody @Valid IcLineSaveParam param){
        String idCityLine = param.getIdCityLine();
        if (StrUtil.isEmpty(idCityLine))
            throw new BusinessException(4001,"城市线路ID[idCityLine]不能为空");
        return success(icLineBiz.IcLineSave(param));
    }

    /**
     * 城际线路信息取得
     * @param param
     * @return
     */
    @PostMapping("/price")
    private DefaultVO icLinePrice(@RequestBody Map<String,String> param){
        String idLine = param.get("idLine");
        if (StrUtil.isEmpty(idLine))
            throw new BusinessException(4001,"线路[idLine]不能为空");
        return success(icLineBiz.getInterCityLinePrice(idLine));
    }

    /**
     * 城际线路信息保存
     * @param param
     * @return
     */
    @PostMapping("/price/save")
    private DefaultVO icLinePriceSave(@RequestBody @Valid IcSaveParam param){
        return success(icLineBiz.icLinePriceSave(param));
    }

//    /**
//     * 城际线路绑定司机
//     * @param param
//     * @return
//     */
//    @PostMapping("/bond/driver")
//    private DefaultVO bindDriver(@RequestBody @Valid IcLineSaveParam param){
//        return success(icLineBiz.IcLineSave(param));
//    }

    /**
     * 城际线路已绑司机列表
     * @param param
     * @return
     */
    @PostMapping("/driver/lst")
    private DefaultVO bindDriverLst(@RequestBody @Valid IcLineSaveParam param){
        return success(icLineBiz.IcLineSave(param));
    }

    /**
     * 城际线路状态更新
     * @param param
     * @return
     */
    @PostMapping("/status")
    private DefaultVO updateCityLineStatus(@RequestBody @Valid IcLineSaveParam param){
        return success(icLineBiz.updateCityLineStatus(param));
    }

    /**
     * 城际线路信息取得(页面使用)
     * @param param
     * @return
     */
    @PostMapping("/city/line/list")
    private DefaultVO cityLineLst(@RequestBody CityLineParam param){
        return success(icLineBiz.findCityLineByCode(param));
    }

    /**
     * 根据城市获取线路列表
     * @param
     * @return
     */
    @PostMapping("/cityLines")
    private DefaultVO getCityLines(@RequestBody Map<String,String> param){
        String cityCode = param.get("cityCode");
        if (StrUtil.isEmpty(cityCode))
            throw new BusinessException(4001,"cityCode不能为空！");
        if (param.get("subBizType") == null) {
            return success(icLineBiz.getCityLines(cityCode));
        } else {
            int subBizType = Integer.parseInt(param.get("subBizType"));
            if (Constants.IcSubBizType.CustomJourney == subBizType) {
                return success(icLineBiz.getCustomCityLines(cityCode));
            }else{
                return success(icLineBiz.getCityLines(cityCode));
            }
        }
    }

//    /**
//     * 线路自动抢单配置数据列表
//     * @param
//     * @return
//     */
//    @PostMapping("/lst/autoGrabInfo")
//    private DefaultVO getAllIntercityLineAutoGrab(@RequestBody Map<String,String> param){
//        return success(icLineBiz.findAllIntercityLineAutoGrab());
//    }

    /**
     * 线路自动抢单配置数据取得
     * @param
     * @return
     */
    @PostMapping("/autoGrab/save")
    private DefaultVO intercityLineAutoGrabSave(@RequestBody IntercityLineAutoGrabParam param){
        return success(IntercityConvertor.INSTANCE.convert2VO(icLineBiz.intercityLineAutoGrabSave(param)));
    }

    /**
     * 线路自动抢单配置数据取得
     * @param
     * @return
     */
    @PostMapping("/autoGrab/idLine")
    private DefaultVO findAutoGrabListByIdLine(@RequestBody Map<String,String> param){
        String idLine = param.get("idLine");
        if (StringUtil.isBlank(idLine))
            throw new BusinessException(4001,"线路ID[id]不能为空");

        List<IntercityLineAutoGrabEntity> list = icLineBiz.findAutoGrabListByIdLine(idLine);

        ArrayList<IntercityLineAutoGrabVO> autoGrabVOArrayList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            IntercityLineAutoGrabEntity entity = list.get(i);
            IntercityLineAutoGrabVO autoGrabVO = IntercityConvertor.INSTANCE.convert2VO(entity);
            JSONObject object = JSONObject.parseObject(entity.getRule());

            JSONObject carpoolGrabRule = object.getJSONObject("carpool");
            AutoGrabRuleVO carpool = JSON.parseObject(carpoolGrabRule.toString(), AutoGrabRuleVO.class);
            autoGrabVO.setCarpool(carpool);
            JSONObject charteredGrabRule = object.getJSONObject("chartered");
            AutoGrabRuleVO chartered = JSON.parseObject(charteredGrabRule.toString(), AutoGrabRuleVO.class);
            autoGrabVO.setChartered(chartered);
            autoGrabVOArrayList.add(autoGrabVO);
        }

        return success(autoGrabVOArrayList);
    }

    /**
     * 线路自动抢单配置数据取得
     * @param
     * @return
     */
    @PostMapping("/autoGrab/info")
    private DefaultVO findAutoGrabInfo(@RequestBody Map<String,String> param){
        String idLineAutoGrab = param.get("id");
        if (StringUtil.isBlank(idLineAutoGrab))
            throw  new BusinessException(4001,"线路自动抢单配置ID[id]不能为空");
        return success(IntercityConvertor.INSTANCE.convert2VO(icLineBiz.findAutoGrabInfo(idLineAutoGrab)));
    }

    /**
     * 城际线路获得绑定司机列表
     * @param param
     * @return
     */
    @PostMapping("/bond/driver/list")
    private DefaultVO getBindDriverList(@RequestBody Map<String,String> param){
        String idFranchisee = param.get("idFranchisee");
        if (StringUtil.isBlank(idFranchisee))
            throw  new BusinessException(4001,"加盟商ID[idFranchisee]不能为空");
        String idCityLine = param.get("idCityLine");
        if (StringUtil.isBlank(idCityLine))
            throw  new BusinessException(4001,"线路ID[idCityLine]不能为空");
        return success(icLineBiz.getBindDriverList(idFranchisee,idCityLine));
    }

    /**
     * 城际线路绑定司机
     * bandKbn:0取消绑定
     * bandKbn:1绑定
     * @param param
     * @return
     */
    @PostMapping("/bond/driver")
    private DefaultVO bindDriver(@RequestBody Map<String,String> param){
        String idDriverLst = param.get("idDriverLst");
        if (StringUtil.isBlank(idDriverLst))
            throw  new BusinessException(4001,"司机ID[idDriverLst]不能为空");
        String idCityLine = param.get("idCityLine");
        if (StringUtil.isBlank(idCityLine))
            throw  new BusinessException(4001,"线路ID[idCityLine]不能为空");
        String bandKbn = param.get("kbn");
        if (StringUtil.isBlank(bandKbn))
            throw  new BusinessException(4001,"处理区分[kbn]不能为空");
        return success(icLineBiz.bindDriver(idDriverLst,idCityLine,NumUtil.getInteger(bandKbn)));
    }

    /**
     * 修改城际线路可抢订单数
     * @param param
     *        idLine
     *        grabNum
     * @return
     */
    @PostMapping("/order/grab/limit")
    private DefaultVO orderGrabLimit(@RequestBody Map<String,String> param){
        String idLine = param.get("idLine");
        if (StringUtil.isBlank(idLine))
            throw  new BusinessException(4001,"线路ID[idLine]不能为空");
        String grabNum = param.get("grabNum");
        int num = 0;
        try {
            if (StringUtil.isBlank(grabNum)) {
                throw  new BusinessException(4001,"抢单最大数[grabNum]不能为空");
            }
            num = Integer.parseInt(grabNum);
        } catch (Exception e) {
            throw  new BusinessException(4001,"抢单最大数[grabNum]必须是数字");
        }
        return success(icLineBiz.orderGrabLimit(idLine, num));
    }

    /**
     * 获取城际线路可抢订单数
     * @param param
     *        idLine
     * @return
     */
    @PostMapping("/order/grab/num")
    private DefaultVO findOrderGrabNum(@RequestBody Map<String,String> param){
        String idLine = param.get("idLine");
        if (StringUtil.isBlank(idLine))
            throw  new BusinessException(4001,"线路ID[idLine]不能为空");
        return success(icLineBiz.findOrderGrabNum(idLine));
    }

}
